package com.zhanggm.springcloud.order.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhanggm.springcloud.order.pojo.Order;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
public class OrderService {

    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "getByIdImpl")
    public Order getById(Integer id){
        Order order = new Order();
        order.setId(id);
        order.setOrderNo("orderno:"+id);
        order.setCreateDate(new Date());
        order.setUserId(new Random().nextInt(1000));
        System.err.println(order.getUserId());
        if(order.getUserId()%2==0){
            throw new RuntimeException();
        }
        return order;
    }

    public Order getByIdImpl(Integer id){
        Order order = new Order();
        order.setOrderNo("111111111");
        order.setUserId(1);
        return order;
    }
}
